<template>
  <div>
    <!-- 信息区域 -->
    <h1>学生信息</h1>
    <el-descriptions class="margin-top" :column="1" border>
      <el-descriptions-item>
        <template slot="label">姓名 </template>
        {{ student_name }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label"> 学号 </template>
        {{ student_num }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label"> 所属学院 </template>
        {{ student_college }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label"> 电话 </template>
        {{ phone }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">邮箱 </template>
        {{ email }}
      </el-descriptions-item>
    </el-descriptions>
    <div class="btn">
      <el-button type="primary" @click="update()">修改信息</el-button>
      <el-button type="primary" @click="goback()">返回</el-button>
    </div>
    <!-- 修改信息弹框 -->
    <el-dialog title="个人信息修改" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="电话" prop="phone">
          <el-input v-model="ruleForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleForm.email"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="ture_updateinfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
export default {
  // 生命周期，创建完成后自动执行
  created() {
    this.getinfo();
  },
  data() {
    return {
      student_name: "",
      student_num: "",
      student_college: "",
      phone: "",
      email: "",
      ruleForm: {},
      dialogVisible: false,
      rules: {
        phone: [{ min: 11, max: 11, message: "电话为11位", trigger: "blur" }],
        email: { type: "email", message: "输入正确格式", trigger: "blur" },
      },
    };
  },
  methods: {
    // 发送获取个人信息的请求
    getinfo() {
      axios
        .get("http://127.0.0.1:8080/my/userinfo")
        .then((res) => {
          console.log(res);
          if (res.data.status == 0) {
            this.student_name = res.data.data[0].student_name;
            this.student_num = res.data.data[0].student_num;
            this.student_college = res.data.data[0].student_college;
            this.phone = res.data.data[0].phone;
            this.email = res.data.data[0].email;
          } else {
            this.$message.error(this.data.message);
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
    // 修改信息按钮的函数
    update() {
      this.dialogVisible = true;
    },
    goback() {
      this.$router.go(-1);
    },

    ture_updateinfo() {
      // console.log(this.ruleForm);
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) return;
        this.dialogVisible = false;
        this.$http
          .post(
            "http://127.0.0.1:8080/my/userinfo",
            this.$qs.stringify(this.ruleForm)
          )
          .then((res) => {
            if (res.data.status == 0) {
              this.getinfo();
            } else {
              this.$message.error(res.data.message);
            }
          })
          .catch((err) => {
            this.$message.error(err);
          });
      });
    },
  },
};
</script>
<style lang="less" scoped>
.info {
  width: 300px;
}
.btn {
  float: right;
  margin: 20px 0;
}
</style>
